﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MemoryFormDeal
{
    public partial class InitForm : Form
    {
        MemoryDo save = new MemoryDo();
        ProgramSave sof = new ProgramSave();
        Bitmap bmp = null;
        Graphics g = null;
        Pen pen = null;
        public InitForm()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            SetStyle(ControlStyles.DoubleBuffer, true);

            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();           

            bmp = new Bitmap(this.DrawView.Width, this.DrawView.Height);
        }

        public void GetReturnInfo(ProgramSave s)
        {
            sof = s;
        }

        public void AddP()
        {
            if (!save.AddNewP(sof))
            {
                MessageBox.Show("内存已满！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                DrawArea();
                DrawView.Refresh();
            }
        }

        public void DelP()
        {
            if(!save.DelP(sof))
            {
                MessageBox.Show("删除失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                DrawArea();
                DrawView.Refresh();
            }
        }

        private void DrawArea()
        {            
            pen = new Pen(Color.Black,2);
            g = Graphics.FromImage(bmp);
            SolidBrush bs;
            int count = 0;
            float size = (float)DrawView.Width/save.GetUnitDrawNum();
            g.Clear(DrawView.BackColor);
            for(int i = 0; i < save.GetUnitDrawNum(); i++)
            {
                g.DrawLine(pen, size*i, 0, size * i, DrawView.Height);
                g.DrawLine(pen, 0, size * i, DrawView.Width, size * i);
            }
            
            for (int i=0;i<save.GetUnitDrawNum();i++)
            {
                for(int j = 0; j < save.GetUnitDrawNum(); j++)
                {
                    bs = new SolidBrush(Color.FromName(save.getunitcolor(count)));
                    g.FillRectangle(bs, (size * j)+1, (size * i)+1, size-2, size-2);
                    count++;
                    bs.Dispose();
                }
            }
            pen.Dispose();
        }

        private void MemoryMax_Paint(object sender, PaintEventArgs e)
        {
            MemoryMax.Text = "最大内存大小：" + save.GetMemorysize() + "KB";
        }

        private void UnitSpace_Paint(object sender, PaintEventArgs e)
        {
           UnitSpace.Text = "块大小：" + save.GetUnitsize() + "KB/块";
        }

        private void DrawView_Paint(object sender, PaintEventArgs e)
        {
            g = DrawView.CreateGraphics();
            g.DrawImage(bmp, 0, 0);
        }

        private void InitForm_Paint(object sender, PaintEventArgs e)
        {
            Text = "内存模拟";
            DrawArea();
            g = DrawView.CreateGraphics();
            g.DrawImage(bmp, 0, 0);
        }

        private void SetMemory_Click(object sender, EventArgs e)
        {
            if(save.GetUnitleftNum()<=0)
            {
                MessageBox.Show("内存已满！","提示",MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }            
            AddForm lform = new AddForm(save.GetUnitleftNum());          
            lform.Show(this);                      
        }

        private void DelMemory_Click(object sender, EventArgs e)
        {
            if(save.IsPEmpty())
            {
                MessageBox.Show("没有运行中进程！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DelForm lform = new DelForm(save.getpsave());
            
            lform.Show(this);
        }

        private void ReSet_Click(object sender, EventArgs e)
        {
            save.ClaarAll();
            DrawArea();
            DrawView.Refresh();
        }

        private void DrawView_MouseDown(object sender, MouseEventArgs e)
        {
            int mx = e.X;
            int my = e.Y;
            int size = DrawView.Width / save.GetUnitDrawNum();
            mx = mx / size;
            my = my / size;
            int t = save.GetUnitDrawNum()*my+mx;          
            unitTip.Show("单元所属:"+save.getunitbelong(t), DrawView);
        }
    }
            
}
